package com.kzn.controller;


import cn.hutool.json.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kzn.entity.Publisher;
import com.kzn.service.IPublisherService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sorclown
 * @since 2022-02-05
 */
@Api("发布者接口")
@RestController
@RequestMapping("/api/publisher")
public class PublisherController {
    private IPublisherService publisherService;

    public PublisherController() {
    }

    @Autowired
    public PublisherController(IPublisherService publisherService) {
        this.publisherService = publisherService;
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public boolean register(@RequestBody Publisher publisher){
        if (null != publisher) {
            return publisherService.save(publisher);
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/logOut",method = RequestMethod.GET)
    public boolean logOut(@RequestBody Integer publisherId){
        if (null != publisherId) {
            QueryWrapper<Publisher> publisherQueryWrapper = new QueryWrapper<>();
            publisherQueryWrapper.eq("id",publisherId);
            return publisherService.remove(publisherQueryWrapper);
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public boolean update(@RequestBody Publisher publisher){
        if (null != publisher.getId()) {
            return publisherService.updateById(publisher);
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/selectById",method = RequestMethod.GET)
    public String selectById(@RequestParam Integer publisherId){
        QueryWrapper<Publisher> wrapper = new QueryWrapper<>();
        wrapper.eq("id",publisherId);
        Publisher publisher = publisherService.getOne(wrapper);
        if (null != publisher) {
            return JSONUtil.parse(publisher).toString();
        }else {
            return null;
        }
    }

    @RequestMapping(value = "/logIn", method = RequestMethod.GET)
    public String logIn(@RequestParam String phone, @RequestParam String password){
        QueryWrapper<Publisher> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Publisher publisher = publisherService.getOne(wrapper);
        if (publisher != null && phone.equals(publisher.getPhone()) && password.equals(publisher.getPassword())){
            return JSONUtil.parse(publisher).toString();
        }else {
            return null;
        }
    }

}
